package com.guizi.halftimesroom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alan on 2016/6/29.
 */
public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private List<List<View>> mListAll;
    private  List<Integer> mRowMaxHeightList ;
    private  void  init(){
        mListAll = new ArrayList<>();
        mRowMaxHeightList = new ArrayList<>();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mListAll.clear();
        mRowMaxHeightList.clear();
        //测量容器的尺寸，在onLayout之前调用
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //定义自适应获取的宽度和高度
        int wrapWidth = 0, wrapHeight = 0;
        //获取每一行的
        int lineWidth = 0, lineHeight = 0;
        int childCount = getChildCount();
        List<View> childList = new ArrayList<>();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            if (lineWidth + childWidth <= widthSize) {
                childList.add(child);
                lineWidth += childWidth;
                lineHeight = Math.max(childHeight, lineHeight);

            } else {
                mListAll.add(childList);
                childList = new ArrayList<>();
                wrapWidth = Math.max(lineWidth, wrapWidth);
                wrapHeight += lineHeight;
                mRowMaxHeightList.add(lineHeight);
                lineWidth = childWidth;
                lineHeight = childHeight;
                childList.add(child);
            }
            if (i == childCount - 1) {
                mListAll.add(childList);
                wrapWidth = Math.max(lineWidth, wrapWidth);
                wrapHeight += lineHeight;
                mRowMaxHeightList.add(lineHeight);
            }
        }
        int lastWidth = widthMode == MeasureSpec.AT_MOST ? wrapWidth : widthSize;
        int lastHeight = heightMode == MeasureSpec.AT_MOST ? wrapHeight : heightSize;
        setMeasuredDimension(lastWidth, lastHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0, right = 0, top = 0, bottom = 0 ;
        for (int i = 0; i <mListAll.size() ; i++) {
            List<View> rowChild = mListAll.get(i);
            for (int j = 0; j <rowChild.size(); j++) {
                View child = rowChild.get(j);
                right = left +child.getMeasuredWidth();
                bottom = top +child.getMeasuredHeight();
                child.layout(left,top,right,bottom);
                left = left+child.getMeasuredWidth();
            }
            top += mRowMaxHeightList.get(i);
            left =0;
        }
    }
}
